//
//  RankVCViewModel.swift
//  zhonglb_iOS
//
//  Created by lxm on 2021/12/22.
//

import UIKit
import RxRelay

class RankVCViewModel: BaseViewModel<Any> {
    // 类型 1 = 普通 2 = 共享
    var type = 1
    
    let tableObj = TableViewObjc()
    
    var dataArray = [RedBagModels.Rank]()
    // 更新时间
    var refreshTime : BehaviorRelay<String?> = BehaviorRelay(value: nil)
    
    func bindAction() {
        tableObj.numberOfRow = { [unowned self] _ in
            self.tableObj.tableView?.backgroundColor = self.dataArray.count == 0 ? .white : .clear
            return self.dataArray.count
        }
        
        tableObj.cellForRowAt = { [unowned self] t, i in
            let cell = t.dequeueReusableCell(withIdentifier: "RankCell") as! RankCell
            cell.backgroundColor = (i.row & 1 == 0) ? UIColor.white : "#F9F8F9".zm.hex()
            let data = self.dataArray[i.row]
//            cell.avatar.zm.setImage(urlStr: data.avatar)
            if i.row < 3 {
                cell.numImg.isHidden = false
                cell.numImg.image = UIImage.init(named: "rank_0\(i.row + 1)")
                cell.numLabel.isHidden = true
                cell.direct.textColor = Style.mainColor
            } else {
                cell.numLabel.isHidden = false
                cell.numImg.isHidden = true
                cell.numLabel.text = String(format: "%02d", i.row + 1)
                cell.numLabel.textColor = "#333333".zm.hex()
                cell.direct.textColor = Style.mainColor
            }
            cell.name.text = data.nickname
            cell.type.text = ( type == 1 ? " 抢红包获得" : "共享红包获得")
            cell.direct.text = "¥\(data.amount.zm.toString(keep: 2, true))"
            
            return cell
        }
        
        tableObj.willDisplay = { [unowned self] t, c, i in
            if i.row == self.dataArray.count - 1 {
                ZMUtil.clipCircle(view: c, Corner: [.bottomLeft, .bottomRight], radius: 12)
            } else {
                c.layer.mask = nil
            }
        }
        
        tableObj.heightForRowAt = { [unowned self] t, i in
            return RankCell.cellHeight
        }
    }
    
    func loadRankList(range : Int, _ compelte : ((_ ok : Bool)->())? = nil) {
        RedBagAPI.red_rank(["range" : range, "type" : type]).rxRequestNullAble(type: [RedBagModels.Rank].self).subscribe(onNext:{ [unowned self] resp in
            if let resp = resp {
                self.dataArray = resp
            } else {
                self.dataArray = []
            }
            self.tableObj.tableView?.mj_header?.endRefreshing()
            self.tableObj.tableView?.reloadData()
            ZMToast.hiddenActivity()
            if let compelte = compelte {
                compelte(true)
            }
        }, onError: { _ in
            self.tableObj.tableView?.mj_header?.endRefreshing()
            if let compelte = compelte {
                compelte(false)
            }
        }, onCompleted: {
            print("complete")
        }).disposed(by: disposeBag)
    }
}

class TableViewObjc : NSObject, UITableViewDelegate, UITableViewDataSource {
    weak var tableView : UITableView?
    
    var numberOfRow : ((_ section : Int)->Int) = { s in
        return 10
    }
    
    var cellForRowAt : ((_ tableView : UITableView, _ indexPath : IndexPath)->UITableViewCell) = { t, i in
        return UITableViewCell()
    }
    
    var didSelectRowAt : (_ tableView: UITableView, _ indexPath: IndexPath) -> Void = { t, i in
        
    }
    
    var heightForRowAt : (_ tableView: UITableView, _ indexPath: IndexPath) -> CGFloat = { t, i in
        return 0
    }
    
    var willDisplay : (_ tableView: UITableView, _ cell: UITableViewCell, _ indexPath: IndexPath) -> Void = { t, c, i in
        
    }
    
    func setTable(_ t : UITableView) {
        t.delegate = self
        t.dataSource = self
        tableView = t
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        numberOfRow(section)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        cellForRowAt(tableView, indexPath)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        didSelectRowAt(tableView, indexPath)
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        willDisplay(tableView, cell, indexPath)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let h = heightForRowAt(tableView, indexPath)
        if h  > 0 {
            return h
        } else {
            return UITableView.automaticDimension
        }
    }
}
